//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.link.base.core.basic.util;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Map;

public class QRCodeUtil {
    static int width = 180;
    static int height = 180;
    private static final int BLACK = -16777216;
    private static final int WHITE = -1;
    private static final Logger logger = LogManager.getLogger(QRCodeUtil.class);

    public QRCodeUtil() {
    }

    public static void tomakeMode(String strJson, String path, String fileName, int width, int height) {
        System.out.println("**********************创建二维码存放路径*********************************");
        System.out.println("**********************创建二维码存放路径*********************************");
        System.out.println("**********************创建二维码存放路径*********************************");
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
        try {
            File qpImg = new File(path + fileName);
            encode(strJson, qpImg, "png", BarcodeFormat.QR_CODE, width, height, (Map)null);
            System.out.println("**********************创建二维码成功*********************************");
            System.out.println("**********************创建二维码成功*********************************");
            System.out.println("**********************创建二维码成功*********************************");
        } catch (Exception var7) {
            logger.error("QRCodeUtil捕获异常: ", var7);
        }
    }

    public static void encode(String contents, File file, String filePostfix, BarcodeFormat format, int width,
                              int height, Map<EncodeHintType, ?> hints) {
        try {
            contents = new String(contents.getBytes("UTF-8"), "ISO-8859-1");
            BitMatrix bMatrix = new QRCodeWriter().encode("生成二维码的内容",BarcodeFormat.QR_CODE, width, height,hints);
            int[] rec = bMatrix.getEnclosingRectangle();
            int resWidth = rec[2] + 1;
            int resHeight = rec[3] + 1;
            BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
            resMatrix.clear();
            for (int i = 0; i < resWidth; i++) {
                for (int j = 0; j < resHeight; j++) {
                    if (bMatrix.get(i + rec[0], j + rec[1])) {
                        resMatrix.set(i, j);
                    }
                }
            }
            width = resMatrix.getWidth();
            height = resMatrix.getHeight();
            BitMatrix bitMatrix = (new MultiFormatWriter()).encode(contents, format, width, height);
            writeToFile(bitMatrix, filePostfix, file);
        } catch (Exception var8) {
            logger.error("QRCodeUtil捕获异常: ", var8);
        }

    }

    public static void writeToFile(BitMatrix matrix, String format, File file) throws IOException {
        BufferedImage image = toBufferedImage(matrix);
        ImageIO.write(image, format, file);
    }

    public static BufferedImage toBufferedImage(BitMatrix matrix) {
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        BufferedImage image = new BufferedImage(width, height, 2);
        for (int x = 0; x < width; ++x) {
            for (int y = 0; y < height; ++y) {
                image.setRGB(x, y, matrix.get(x, y) ? -16777216 : -1);
            }
        }
        return image;
    }

}
